<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo;
use Biz\model\GoodsExtendInfo;
use Doraemon\model\admTag;
use Doraemon\model\Spu as mSpu;

$goodsId = 0;

$goodsInfo = GoodsBasicInfo::get('*', [
	'goods_id[>]' => $goodsId,
]);


while ($goodsInfo) {
	$goodsId = $goodsInfo['goods_id'];


	$goodsExtend = GoodsExtendInfo::getInfo($goodsId);
	$wikiInfo = mSpu::getSpuInfo($goodsInfo['wiki_id']);
	if ($wikiInfo['wiki_type'] == mSpu::WIKI_TYPE_SHOP) {

		$wikiData = [
			'title' => $goodsInfo['title'],
			'brand_id' => $goodsInfo['brand_id'],
			'category_id' => $goodsInfo['category_id'],
			'last_category_id' => $goodsInfo['last_category_id'],
//			'third_category_id' => $goods[$goodsInfo['goods_id']]['third_category_id'],
			'cover_img' => $goodsInfo['spec_img'],
//			'imgs' => json_encode( $goods[$goodsInfo['goods_id']]['images_content_hash']),
			'min_price' => $goodsInfo['min_price'],
			'max_price' => $goodsInfo['max_price'],
			'tags' => implode(',',  array_column(admTag::getTagByIds(explode(',',  $goodsExtend['style_content']) ,['tag']), 'tag'))
		];

//		var_dump($wikiData);
		$updateData = [];
		foreach ($wikiData as $field => $data) {
			if ($data != $wikiInfo[$field]) {
				$updateData[$field] = $data;
			}
		}
		if ($updateData) {
			mSpu::update($updateData, ['id' => $goodsInfo['wiki_id']]);

			\Doraemon\tools\Log::debug('syncWiki_id_' . $goodsInfo['wiki_id'], [
				'currentData' => $wikiInfo,
				'updateData' => $updateData
			]);

			\Doraemon\service\Search::updateProductById($goodsInfo['wiki_id']);

			echo "wiki_id: " . $goodsInfo['wiki_id'] . " goods_id: " . $goodsInfo['goods_id'] . " updateData: " . json_encode($updateData) . " \r\n\r\n";
		}

	}

	$goodsInfo = GoodsBasicInfo::get('*', [
		'goods_id[>]' => $goodsId,
	]);
}
